package com.chenyaowei.web.controller.order;

import com.chenyaowei.common.annotation.Log;
import com.chenyaowei.common.core.controller.BaseController;
import com.chenyaowei.common.core.domain.AjaxResult;
import com.chenyaowei.common.core.page.TableDataInfo;
import com.chenyaowei.common.enums.BusinessType;
import com.chenyaowei.common.utils.poi.ExcelUtil;
import com.chenyaowei.order.domain.CywOrder;
import com.chenyaowei.order.enums.OrderStatusEnum;
import com.chenyaowei.order.service.ICywOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 订单Controller
 *
 * @author 陈耀威
 * @date 2023-12-03
 */
@RestController
@RequestMapping("/order/info")
public class CywOrderController extends BaseController
{
    @Autowired
    private ICywOrderService cywOrderService;

    @PutMapping("/cancel/{orderSn}")
    public AjaxResult cancelOrder(@PathVariable("orderSn") String orderSn)
    {
        return toAjax(cywOrderService.cancelOrder(orderSn, OrderStatusEnum.CANCLED.getCode()));
    }

    @GetMapping("/front/list")
    public TableDataInfo frontList(CywOrder cywOrder)
    {
        startPage();
        List<CywOrder> list = cywOrderService.selectListForFront(cywOrder);
        return getDataTable(list);
    }

    /**
     * 查询订单列表
     */
//    @PreAuthorize("@ss.hasPermi('order:order:list')")
    @GetMapping("/list")
    public TableDataInfo list(CywOrder cywOrder)
    {
        startPage();
        List<CywOrder> list = cywOrderService.selectCywOrderList(cywOrder);
        return getDataTable(list);
    }

    /**
     * 导出订单列表
     */
    @PreAuthorize("@ss.hasPermi('order:order:export')")
    @Log(title = "订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CywOrder cywOrder)
    {
        List<CywOrder> list = cywOrderService.selectCywOrderList(cywOrder);
        ExcelUtil<CywOrder> util = new ExcelUtil<CywOrder>(CywOrder.class);
        util.exportExcel(response, list, "订单数据");
    }

    /**
     * 获取订单详细信息
     */
//    @PreAuthorize("@ss.hasPermi('order:order:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(cywOrderService.selectCywOrderById(id));
    }

    /**
     * 新增订单
     */
    @PreAuthorize("@ss.hasPermi('order:order:add')")
    @Log(title = "订单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CywOrder cywOrder)
    {
        return toAjax(cywOrderService.insertCywOrder(cywOrder));
    }

    /**
     * 修改订单
     */
//    @PreAuthorize("@ss.hasPermi('order:order:edit')")
    @Log(title = "订单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CywOrder cywOrder)
    {
        return toAjax(cywOrderService.updateCywOrder(cywOrder));
    }

    /**
     * 删除订单
     */
//    @PreAuthorize("@ss.hasPermi('order:order:remove')")
    @Log(title = "订单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(cywOrderService.deleteCywOrderByIds(ids));
    }
}
